package at.fhooe.mc.duel_game;

public class ByteConverters {

	public static final byte[] makeByte4FromInt(int i) {
		return new byte[] { (byte) (i >> 24), (byte) (i >> 16),
				(byte) (i >> 8), (byte) i };
	}

	public static final byte[] makeByte8FromLong(long l) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			b[7 - i] = (byte) (l >>> (i * 8));
		}

		return b;
	}

	public static final int makeIntFromByte4(byte[] b) {
		return b[0] << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8
				| (b[3] & 0xff);
	}

	public static final long makeLongFromByte8(byte[] b) {
		long l = 0;

		for (int i = 0; i < 8; i++) {
			l <<= 8;
			l ^= (long) b[i] & 0xFF;
		}

		return l;
	}
}
